<?php

class UploadedFileForm {

  /**
   * Save the file to the specified path
   * @return boolean TRUE on success
   */
  private $fileForm = null;

  function __construct($fileForm=null) {
    if (!is_null($fileForm)) {
      $this->fileForm = !empty($_FILES[$fileForm]) ? $_FILES[$fileForm] : null;
    }
  }

  function save($path) {
    $result = array();
    if (!is_null($this->fileForm)) {
      $input = null;
      if (is_array($this->fileForm['tmp_name'])) {
        $input = $this->fileForm;
      } else {
        $input = array("temp_name" => array($this->fileForm['tmp_name']), "name" => array($this->fileForm['name']), "size" => array($this->fileForm['size']));
      }
      for ($i = 0; $i < sizeof($input['tmp_name']); $i++) {
        if (move_uploaded_file($input['tmp_name'][$i], dirname(__FILE__)."/".$path."/".$input['name'][$i])) {
          $result[$i] = array("result" => true, "file_name" => $input['name'][$i], "file_size" => $input['size'][$i], "file_path" => "services/".$path . "/" . $input['name'][$i]);
        } else {
          $result[$i] = array("result" => false, "file_name" => $input['name'][$i], "file_size" => $input['size'][$i], "file_path" => "");
        }
      }
    } else {
      $result[0]= array("result"=>false, "details"=>"Form input is null");
    }
    return $result;
  }

  function getName($index=0) {
    return!is_null($fileForm) ? (is_array($fileForm['name']) ? $fileForm['name'][$index] : $fileForm['name']) : "";
  }

  function getSize($index=0) {
    return!is_null($fileForm) ? (is_array($fileForm['size']) ? $fileForm['size'][$index] : $fileForm['size']) : "";
  }

  public function getFileForm() {
    return $this->fileForm;
  }

  public function setFileForm($fileForm) {
    $this->fileForm = $fileForm;
  }

}

$temp = $_FILES;

$upload = new UploadedFileForm("fileUpload1");
$result = $upload->save("temp");

echo '<textarea>';
echo json_encode($result);
echo '</textarea>';
?>
